using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

public class ButtonManager : MonoBehaviour
{
    public List<ButtonPos> AllButtons;
    public ButtonPanel firstPanel;

    private void Start()
    {
        foreach(ButtonPos button in AllButtons)
        {
            button.InitializeButton();
        }
        InitializeButtonManage();
    }

    public void InitializeButtonManage()
    {
        firstPanel.InitializeButtonPanel();
    }

    public void ChangeBackImage(Vector2Int pos, Sprite image)
    {
        ButtonPos button = FindButton(pos);
        if (button.isHide)
            button.ResetButton();
        Color preColor = button.backImage.color;
        button.backImage.color = preColor;

        button.backImage.sprite = image;
    }

    public void ChangeText(Vector2Int pos, string text)
    {
        ButtonPos button = FindButton(pos);
        if (button.isHide)
            button.ResetButton();
        button.textComponent.gameObject.SetActive(true);

        button.textComponent.text = text;
    }

    public void ChangeTextColor(Vector2Int pos, Color color)
    {
        ButtonPos button = FindButton(pos);
        if (button.isHide)
            button.ResetButton();
        button.textComponent.gameObject.SetActive(true);

        button.textComponent.color = color;
    }

    public void ChangeOnClickEvent(Vector2Int pos, UnityAction unityEvent)
    {
        ButtonPos button = FindButton(pos);
        if (button.isHide)
            button.ResetButton();
        button.button.onClick.AddListener(unityEvent);
    }

    public void RemoveEvent(Vector2Int pos)
    {
        var button = FindButton(pos);
        if (button.isHide)
            button.ResetButton();
    }

    public void ClearAllEevent()
    {
        foreach (ButtonPos button in AllButtons)
        {
            button.button.onClick.RemoveAllListeners();
        }
    }

    public void HideButton(Vector2Int pos)
    {
        FindButton(pos).HideLinkComponent();
    }

    public void HideButton(List<Vector2Int> pos)
    {
        foreach(ButtonPos buttonPos in AllButtons)
        {
            foreach(Vector2Int p in pos)
            {
                if(buttonPos.pos == p)
                {
                    buttonPos.HideLinkComponent();
                }
            }
        }
    }

    public ButtonPos FindButton(Vector2Int pos)
    {
        foreach(ButtonPos buttonPos in AllButtons)
        {
            if (buttonPos.pos.x == pos.x && buttonPos.pos.y == pos.y)
                return buttonPos;
        }

        return null;
    }
}
